<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="询价比价名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入询价比价名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购计划编号" prop="planNumber">
        <el-input
          v-model="queryParams.planNumber"
          placeholder="请输入采购计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料/产品编号" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料/产品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cg:comparison:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cg:comparison:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['cg:comparison:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="comparisonList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="询价比价名称" align="center" prop="name"/>
      <el-table-column label="采购计划编号" align="center" prop="planNumber"/>
      <el-table-column label="采购计划名称" align="center" prop="planName"/>
      <el-table-column label="物料/产品编号" align="center" prop="itemCode"/>
      <el-table-column label="物料/产品名称" align="center" prop="itemName"/>
      <el-table-column label="采购数量" align="center" prop="quantity"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cg:comparison:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cg:comparison:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购管理-询价比价主对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="询价比价名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入询价比价名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购计划编号" prop="planNumber">
              <el-input v-model="form.planNumber" readonly="readonly" placeholder="请选择采购计划">
                <el-button slot="append" @click="handleSelectPlan" icon="el-icon-search"></el-button>
              </el-input>
              <PlanSelect ref="PlanSelect" @onSelected="onPlanSelected"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="采购计划名称" prop="planName">
              <el-input v-model="form.planName" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料/产品编号" prop="itemCode">
              <el-input v-model="form.itemCode" readonly="readonly"
                        :placeholder="form.planId?'请选择物料/产品编号':'请选择采购计划'">
                <el-button slot="append" @click="handleSelectItem" icon="el-icon-search" v-if="form.planId"></el-button>
              </el-input>
              <ItemSelect ref="itemSelect" :planId="form.planId" @onSelected="onItemSelected"
                          v-if="form.planId"></ItemSelect>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="物料/产品名称" prop="itemName">
              <el-input v-model="form.itemName" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="specification">
              <el-input v-model="form.specification" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单位" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="期初价格" prop="initialPrice">
              <el-input v-model="form.initialPrice" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="参考价格" prop="referencePrice">
              <el-input v-model="form.referencePrice" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购数量" prop="quantity">
              <el-input v-model="form.quantity" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-divider v-if="form.id !=null" content-position="center">供应商报价</el-divider>
      <el-card shadow="always" v-if="form.id !=null" class="box-card">
        <el-row style="margin-bottom: 20px;">
          <el-col :span="5">
            <el-button type="primary" @click="addQuotation" style="margin-left: 20px;" v-hasPermi="['cg:comparison:remove']">点击添加报价</el-button>
          </el-col>
        </el-row>
        <el-table border :data="quotationList">
          <el-table-column label="供应商编号" align="center" prop="vendorCode"/>
          <el-table-column label="供应商名称" align="center" prop="vendorName"/>
          <el-table-column label="供应商报价" align="center" prop="vendorQuotation"/>
          <el-table-column label="总价" align="center" prop="total"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="editQuotation(scope.row)"
                v-hasPermi="['cg:comparison:remove']"
              >修改报价
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="deleteQuotation(scope.row)"
                v-hasPermi="['cg:comparison:remove']"
              >删除报价
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="添加供应商报价" :visible.sync="quotationOpen" width="1100px" append-to-body>
      <el-form ref="quotationForm" :model="quotationForm" :rules="quotationFormRules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商编号" prop="vendorCode">
              <el-input v-model="quotationForm.vendorCode" readonly="readonly" placeholder="请选择供应商" >
                <el-button slot="append" @click="handleSelectVendor" icon="el-icon-search"></el-button>
              </el-input>
              <VendorSelect ref="vendorSelect" @onSelected="onVendorSelected" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="vendorName">
              <el-input v-model="quotationForm.vendorName" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商报价" prop="vendorQuotation">
              <el-input-number v-model="quotationForm.vendorQuotation" :precision="2" :step="1" :min="0" @change="countTotal"
                               style="width: 100%"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总价" prop="total">
              <el-input v-model="quotationForm.total" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitQuotationForm">确 定</el-button>
        <el-button @click="cancelAddQuotation">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listComparison, getComparison, delComparison, addComparison, updateComparison} from "@/api/mes/cg/comparison";
import {listLine, getLine, addLine, updateLine, delLine} from "@/api/mes/cg/comparisonLine";
import {getMdItem} from "@/api/mes/md/mdItem";
import PlanSelect from "@/views/mes/cg/order/planSelect.vue";
import ItemSelect from "@/views/mes/cg/comparison/itemSelect.vue";
import VendorSelect from "@/components/vendorSelect/single.vue";

export default {
  name: "Comparison",
  components: {VendorSelect, ItemSelect, PlanSelect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购管理-询价比价主表格数据
      comparisonList: [],
      //供应商报价列表
      quotationList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      quotationOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        planNumber: null,
        itemCode: null
      },
      // 表单参数
      form: {},
      quotationForm: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "询价比价名称不能为空", trigger: "blur"}
        ],
        planNumber: [
          {required: true, message: "采购计划编号不能为空", trigger: "blur"}
        ],
        itemCode: [
          {required: true, message: "物料/产品编号不能为空", trigger: "blur"}
        ]
      },
      quotationFormRules: {
        vendorCode: [
          {required: true, message: "供应商编号不能为空", trigger: "blur"}
        ],
        vendorQuotation: [
          {required: true, message: "供应商报价不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购管理-询价比价主列表 */
    getList() {
      this.loading = true;
      listComparison(this.queryParams).then(response => {
        this.comparisonList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelAddQuotation() {
      this.quotationOpen = false;
      this.resetQuotationForm();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        remarks: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        planId: null,
        planNumber: null,
        planName: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        initialPrice: null,
        referencePrice: null,
        quantity: null
      };
      this.resetForm("form");
    },
    resetQuotationForm(){
      this.quotationForm = {
        lineId: null,
        priceComparisonId: this.form.id,
        vendorId: null,
        vendorCode: null,
        vendorName: null,
        vendorQuotation: null,
        total: null
      };
      this.resetForm("quotationForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购管理-询价比价主";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getComparison(id).then(response => {
        this.form = response.data;
        this.getQuotationList();
        this.open = true;
        this.title = "修改采购管理-询价比价主";
      });
    },
    handleSelectPlan() {
      this.$refs.PlanSelect.showFlag = true;
      this.$refs.PlanSelect.selectedPlanId = undefined;
    },
    onPlanSelected(obj) {
      if (obj != undefined && obj != null) {
        this.form.planId = obj.planId;
        this.form.planNumber = obj.planNumber;
        this.form.planName = obj.planName;
        this.form.planDate = obj.planDate;
        this.form.materialDeadline = obj.materialDeadline;
      }
    },
    handleSelectItem() {
      this.$refs.itemSelect.showFlag = true;
      this.$refs.itemSelect.selectedItemId = undefined;
      this.$refs.itemSelect.selectedRows = undefined;
      this.$refs.itemSelect.getList();
    },
    onItemSelected(obj) {
      console.log(obj)
      getMdItem(obj.itemId).then(response => {
        this.form.itemId = response.data.itemId;
        this.form.itemCode = response.data.itemCode;
        this.form.itemName = response.data.itemName;
        this.form.specification = response.data.specification;
        this.form.unitOfMeasure = response.data.unitOfMeasure;
        this.form.initialPrice = response.data.initialPrice;
        this.form.referencePrice= response.data.referencePrice;
        this.form.quantity= obj.quantity;
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateComparison(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addComparison(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除询价比价数据？').then(function () {
        return delComparison(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cg/comparison/export', {
        ...this.queryParams
      }, `comparison_${new Date().getTime()}.xlsx`)
    },
    deleteQuotation(row){
      this.$modal.confirm('是否确认供应商报价数据？').then(function () {
        return delLine(row.lineId);
      }).then(() => {
        this.getQuotationList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    addQuotation(){
      this.resetQuotationForm();
      this.quotationOpen = true;
    },
    editQuotation(row){
      getLine(row.lineId).then(response => {
        this.quotationForm = response.data;
        this.quotationOpen = true;
      })
    },
    getQuotationList(){
      listLine({'priceComparisonId':this.form.id}).then(response => {
        this.quotationList = response.rows;
      })
    },
    //供应商选择
    handleSelectVendor(){
      this.$refs.vendorSelect.showFlag = true;
    },
    //供应商选择弹出框
    onVendorSelected(obj){
      if(obj != undefined && obj != null){
        this.quotationForm.vendorId = obj.vendorId;
        this.quotationForm.vendorCode = obj.vendorCode;
        this.quotationForm.vendorName = obj.vendorName;
      }
    },
    submitQuotationForm(){
      this.$refs["quotationForm"].validate(valid => {
        if (valid) {
          if (this.quotationForm.lineId != null) {
            updateLine(this.quotationForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.quotationOpen = false;
              this.getQuotationList();
            });
          } else {
            addLine(this.quotationForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.quotationOpen = false;
              this.getQuotationList();
            })
          }
        }
      });
    },
    countTotal(value){
      if(value != null && value != undefined){
        this.quotationForm.total = value * this.form.quantity;
      }
    }
  }
};
</script>
